<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/7
 * Time: 10:43
 */

namespace app\mobile\controller;


class Index extends Base
{
    private $tpl;
    private $domain;
    private $second;
    public function _initialize()
    {
        $this->domain = request()->host();
        $this->second = str_replace('.'.config('url_domain_root'),'',$this->domain);
        $this->getSiteInfo($this->second);
        if(cookie('cityInfo') && $this->second != config('url_domain_root') && $this->second != 'm'){
            $this->tpl = 'house';
            $this->menuSeo = false;
        }else{
            $this->tpl = 'index';
        }

        parent::_initialize();
    }
    public function index()
    {
        $slide = action('home/Poster/index',['id'=>4,'num'=>5,'city'=>$this->cityInfo['id']],'event');//切换图广告
        if($this->tpl == 'house'){
            $this->count();
        }else{
            $this->assign('title','选择城市');
        }
        $this->assign('siteInfo',$this->cityInfo);
        $this->assign('slides',$slide);
        $this->assign('time',time());
        return $this->fetch($this->tpl);
    }
    private function count(){
        $where['city']   = $this->cityInfo['id'];
        $where['status'] = 1;
        $group_where['city']   = $this->cityInfo['id'];
        $group_where['status'] = 1;
        $group_where['begin_time'] = ['lt',time()];
        $group_where['end_time']   = ['gt',time()];
        //团购楼盘
        $group            = model('house_group')->where($group_where)->field('id,title,price,img,buy_num,youhui')->order('id desc')->limit(4)->select();
       //推荐楼盘
        $where['cate_id'] = 1;
        $position_mod     = model('position');
        $house_position   = $position_mod->where($where)->order('ordid asc,id desc')->limit(5)->select();
        //最新二手房
        $field = 'id,title,img,room,ting,chu,wei,yang,price,total_price,mianji,contact,contact_phone';
        $second_position  = model('second_house')->field($field)->where(['status'=>1,'city'=>$this->cityInfo['id']])->order('ordid asc,id desc')->limit(5)->select();
        $this->assign('group',$group);
        $this->assign('house_position',$house_position);
        $this->assign('second_position',$second_position);
        $this->assign('cityid',$this->cityInfo['id']);
    }
    private function getSiteInfo($second=''){
        $second = str_replace('.m','',$second);
        $cityInfo = cookie('cityInfo');
        if($cityInfo && $cityInfo['domain']==$second){
            $this->cityInfo = $cityInfo;
            return $cityInfo;
        }else{
            if($second == 'm'){
                if($cityInfo){
                    $this->cityInfo = $cityInfo;
                    return $cityInfo;
                }else{
                    if($info = $this->getIpToCity()){
                        cookie('cityInfo',$info);
                        $this->cityInfo = $info;
                        $this->redirect('Index/index@'.$info['domain'].'.m');
                    }
                    return false;
                }
            }else{
                    $info = db('city')->where(['domain'=>$second])->find();
                    cookie('cityInfo',$info);
                    $this->cityInfo = $info;
                    return $info;
            }
        }

    }
    private function getIpToCity(){
        $city_arr = \util\Ip::find();
        $city     = $city_arr[2];
        $obj      = db('city');
        $info     = [];
        $city && $info   = $obj->where(['status'=>1,'name'=>['like',$city.'%']])->find();
        if(!$info){
            $info = $obj->where(['default'=>1,'status'=>1])->find();
        }
        return $info;
    }
}